//
// Created by leoking870 on 2017/11/3.
// Copyright (c) 2017 leoking870. All rights reserved.
//

#import "WorkFlowManager.h"
#import "YKTurnOverUserModel.h"
#import "YKPopupView.h"
#import "YKTurnOverViewController.h"


@implementation WorkFlowManager {

}

+ (void)getNextAssigneeWithBusinessID:(NSString *)businessID
                           completion:(void(^)(NSArray<YKTurnOverUserModel *> *nextAssignees, YKTurnOverUserTaskModel *nextTask, NSError *error))completion {
    if (!businessID) {
        if (completion){
            completion(nil, nil, [NSError errorWithDomain:@"WorkFlowManager" code:1000 userInfo:@{NSLocalizedFailureReasonErrorKey:@"businessID 不存在"}]);
        }
        return;
    }
    [TFHTTPMANAER POST:@"workExamine/nextAssignee.htm" parameters:@{@"businessId":businessID} success:^(id responseObject) {
        NSArray<YKTurnOverUserModel*> *users = [NSArray yy_modelArrayWithClass:[YKTurnOverUserModel class] json:[responseObject valueForKeyPath:@"userList.mapSel"]];
        NSArray<YKTurnOverUserTaskModel*> *nextTasks = [NSArray yy_modelArrayWithClass:[YKTurnOverUserTaskModel class] json:[responseObject valueForKeyPath:@"taskList"]];
        if (completion){
            completion(users, nextTasks.firstObject, nil);
        }

    } failure:^(id responseObject, NSError *error) {
        if ([error.localizedFailureReason isEqualToString:@"未创建工作流"])
        {
            [self createWorkflowWithBusinessID:businessID completion:^(NSError *err) {
                if (!err){
                    [self getNextAssigneeWithBusinessID:businessID completion:completion];
                }
                else {
                    if (completion){
                        completion(nil, nil, err);
                    }
                }
            }];
        }
        else {
            if (completion){
                completion(nil, nil, error);
            }
        }
    }];
}

+ (void)getPreTaskWithBusinessID:(NSString *)businessID completion:(void (^)(NSArray<YKTurnOverUserModel *> *nextAssignees, NSError *error))completion {
    if (!businessID) {
        if (completion){
            completion(nil, [NSError errorWithDomain:@"WorkFlowManager" code:1000 userInfo:@{NSLocalizedFailureReasonErrorKey:@"businessID 不存在"}]);
        }
        return;
    }
    [TFHTTPMANAER POST:@"workExamine/preTask.htm" parameters:@{@"businessId":businessID} success:^(id responseObject) {
        NSArray *preTaskMap = responseObject[@"preTaskMap"];
        if (preTaskMap.count){
            NSDictionary *dict = preTaskMap.firstObject;
            NSArray<YKTurnOverUserModel*> *users = [NSArray yy_modelArrayWithClass:[YKTurnOverUserModel class] json:[dict valueForKey:@"mapSel"]];
            if (completion){
                completion(users, nil);
            }
        }
        else {
            if (completion){
                completion(nil, nil);
            }
        }
    } failure:^(id responseObject, NSError *error) {
        if (completion){
            completion(nil, error);
        }
    }];
}


+ (void)repositionApproveWithBusinessID:(NSString *)businessID fromViewController:(UIViewController *)viewController completion:(void(^)(BOOL succcess))completion{
    if (!businessID) {
        [viewController showHint:@"businessID 不存在"];
        if (completion){
            completion(NO);
        }
        return;
    }
    YKTurnOverViewController *vc = [YKTurnOverViewController new];
    vc.m_id = businessID;
    vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
        [WorkFlowManager completeWorkFlowWithBusinessID:businessId mark:text nextAssigneeID:nextId completion:^(NSError *error) {
            if (!error){
                [viewController showHint:@"转审成功"];
                if(completion){
                    completion(YES);
                }
            }
            else {
                [viewController showHint:error.localizedFailureReason];
                if(completion){
                    completion(NO);
                }
            }
        }];
    };
    [YKPopupView showPopupViewWithViewController:vc];
}

+ (void)submitToApproveWithBusinessID:(NSString *)businessID fromViewController:(UIViewController *)viewController{
    if (!businessID) {
        [viewController showHint:@"businessID 不存在"];
        return;
    }
    YKTurnOverViewController *vc = [YKTurnOverViewController new];
    vc.m_id = businessID;
    vc.type = YKTurnOverTypeTurnOver;
    vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
        [WorkFlowManager completeWorkFlowWithBusinessID:businessId mark:text nextAssigneeID:nextId completion:^(NSError *error) {
            if (!error){
                [viewController showHint:@"提交审批成功"];
                [viewController.navigationController popViewControllerAnimated:YES];
            }
            else {
                [viewController showHint:error.localizedFailureReason];
            }
        }];
    };
    [YKPopupView showPopupViewWithViewController:vc];
}

+ (void)completeWorkFlowWithBusinessID:(NSString *)businessID
                                  mark:(NSString *)mark
                        nextAssigneeID:(NSString*)nextId
                            completion:(void (^)(NSError *error))completion {
    if (!businessID) {
        if (completion) {
            completion([NSError errorWithDomain:@"WorkFlowManager" code:1000 userInfo:@{NSLocalizedFailureReasonErrorKey:@"businessID 不存在"}]);
        }
        return;
    }
    [TFHTTPMANAER POST:@"workExamine/completeWorkExamine.htm"
            parameters:@{@"mark":mark,@"businessId":businessID,@"nextId":nextId}
                   HUD:@"处理中..."
               success:^(id responseObject) {
        if (completion){
            completion(nil);
        }
    }
               failure:^(id responseObject, NSError *error) {
                if (completion){
                    completion(error);
                }
    }];
}

+ (void)createWorkflowWithBusinessID:(NSString *)businessID
                          completion:(void(^)(NSError *error))completion {
    if (!businessID) {
        if (completion) {
            completion([NSError errorWithDomain:@"WorkFlowManager" code:1000 userInfo:@{NSLocalizedFailureReasonErrorKey:@"businessID 不存在"}]);
        }
        return;
    }
    [TFHTTPMANAER POST:@"workExamine/createWorkExamine.htm" parameters:@{@"businessId":businessID} success:^(id responseObject) {
        if (completion){
            completion(nil);
        }
    } failure:^(id responseObject, NSError *error) {
        if (completion){
            completion(error);
        }
    }];
}

@end
